<?php
require_once('config.inc.php');
$IP = dirname( __FILE__ );

/*****************************
* Variables
*****************************/
$GLOBALS['fast'] = true;
DEFINE('API_ROOT','http://en.wikinews.org/w/api.php?');
DEFINE('INDEX_ROOT','http://en.wikinews.org/w/index.php?');



/*****************************
* DB Connect
*****************************/

$GLOBALS['con'] = mysqli_connect(DB_HOST,DB_USER,DB_PASS) or die("Cannot Connect to DB");
mysqli_select_db($GLOBALS['con'], DB_DB);
mysqli_autocommit($GLOBALS['con'], TRUE);

// make sure all communication is in utf-8
mysqli_set_charset($GLOBALS['con'], 'utf8');
mysqli_query($GLOBALS['con'], "SET NAMES 'utf8'");


/*****************************
* Shared Subs
*****************************/

function curlIt($url, $post=0, $poststr=""){
	if(!$GLOBALS['fast']){randomSleep();}								// Abuse protection
	$UA = "Mozilla/4.0 (compatible; WikinewsBot/1.0)";
        $ch = curl_init($url);
        //curl_setopt($ch, CURLOPT_VERBOSE, 1); //--Debug only
	$cookie_file = "/tmp/cookiejar.txt";
	curl_setopt($ch,CURLOPT_COOKIEJAR, $cookie_file);
	curl_setopt($ch,CURLOPT_COOKIEFILE, $cookie_file);
        if($post){
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $poststr);
        }
        curl_setopt($ch, CURLOPT_USERAGENT, $UA);
        ob_start();
        curl_exec($ch);
        curl_close($ch);
        $retHTML = ob_get_contents();
        ob_end_clean();

        return($retHTML);
}

function randomSleep(){
	//Prevent a major hammering from bots
	// Rand 1 to 5, with .1 incriments
        $x = (double) ((mt_rand(10, 50)) / 10) * 1000000;
        usleep($x);
}

